package com.threeonemeeting.task;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;

import com.snda.lib.http.HttpUtil;
import com.snda.lib.http.ICallBack;
import com.threeonemeeting.DataCenter;
import com.threeonemeeting.model.EnrollFieldInfo;
import com.threeonemeeting.model.EnrollFieldName;
import com.threeonemeeting.model.FullMeetingInfo;

// 获取会议的更详细的信息，比如用户是否已经报名
public class GetMeetingMoreDetailTask extends ApplicationTask {

	public int nMeetingId = 0;

	public GetMeetingMoreDetailTask(Context context, ICallBack callback) {
		super(context, callback);

		this.bCached = true;
		this.nTaskType = HttpConst.TASKTYPE_MOREMEETINGDETAIL;
		this.strUrl = "http://new.31huiyi.com/json/GetEventDetailHandler.ashx";
	}

	// 具体的task实现该方法，对响应报文进行解析
	protected Map<String, Object> parseResponse(String strResponse) {

		Map<String, Object> mapData = new HashMap<String, Object>();
		mapData.put(HttpConst.KEY_TASK_TYPE, nTaskType);
		mapData.put(HttpUtil.KEY_ERROR_CODE, HttpUtil.ERROR_SUCCESS);

		JSONObject jsonResp = null;
		try {
			jsonResp = new JSONObject(strResponse);
		} catch (Exception e) {
			setErr(HttpUtil.ERROR_SERVER_PACKET, mapData);
			return mapData;
		}

		try {
			// 获取 "Setting"部分的内容
			String strSuccess = jsonResp.getString("success");
			if (strSuccess == null) {
				setErr(HttpUtil.ERROR_SERVER_PACKET, mapData);
				return mapData;
			}

			if (strSuccess.equalsIgnoreCase("true") == false) {
				setErr(HttpUtil.ERROR_SERVER_PACKET, mapData);
				return mapData;
			}

		} catch (JSONException ex) {
			setErr(HttpUtil.ERROR_SERVER_PACKET, mapData);
			return mapData;
		}

		JSONObject dataJson = null;
		try {
			dataJson = jsonResp.getJSONObject("data");
			if (dataJson == null) {
				setErr(HttpUtil.ERROR_SERVER_PACKET, mapData);
				return mapData;
			}
		} catch (JSONException ex) {
			setErr(HttpUtil.ERROR_SERVER_PACKET, mapData);
			return mapData;
		}

		FullMeetingInfo fInfo = DataCenter.getInstance().listFullMeeting
				.getFullMeetingInfo(nMeetingId);
		if (fInfo == null) {
			setErr(HttpUtil.ERROR_SERVER_PACKET, mapData);
			return mapData;
		}

		try {
			fInfo.bCloseTopic = dataJson.getBoolean("closetopic");
		} catch (JSONException ex) {
			fInfo.bCloseTopic = true;
		}

		try {
			fInfo.bCloseJoin = dataJson.getBoolean("closejoin");
		} catch (JSONException ex) {
			fInfo.bCloseJoin = true;
		}

		try {
			fInfo.bIsJoined = dataJson.getBoolean("IsJoined");
		} catch (JSONException ex) {
			fInfo.bIsJoined = true;
		}

		try {
			fInfo.bIsFaved = dataJson.getBoolean("IsFaved");
		} catch (JSONException ex) {
			fInfo.bIsFaved = true;
		}

		try {
			fInfo.nDocuments = dataJson.getInt("documents");
		} catch (JSONException ex) {
			fInfo.nDocuments = 0;
		}

		try {
			mapData.put("topics", dataJson.getInt("topics"));
		} catch (JSONException ex) {
			mapData.put("topics", 0);
		}

		try {

			fInfo.listRelativeMeeting.clear();

			// 获取相关的会议
			JSONArray relativeJson = dataJson.getJSONArray("ralates");
			if (relativeJson != null) {
				for (int i = 0, n = relativeJson.length(); i < n; i++) {
					JSONObject jData = relativeJson.getJSONObject(i);

					FullMeetingInfo fullInfo = ProtocolHelper
							.getFullMeetingInfo(jData);
					if (fullInfo != null) {
						DataCenter.getInstance().listFullMeeting
								.addFullMeetingInfo(fullInfo);
						fInfo.listRelativeMeeting.add(fullInfo.nId);
					}
				}
			}

		} catch (JSONException ex) {
			setErr(HttpUtil.ERROR_SERVER_PACKET, mapData);
			return mapData;
		}

		try {

			DataCenter.getInstance().listEnrollField.clear();

			// 检查加入字段
			JSONObject joinedJson = dataJson.getJSONObject("joinfield");
			if (joinedJson != null) {
				ArrayList<EnrollFieldName> listEnrollFieldName = DataCenter
						.getInstance().listEnrollFieldName;
				for (int i = 0; i < listEnrollFieldName.size(); i++) {
					EnrollFieldInfo info = new EnrollFieldInfo();

					info.nMeetingId = nMeetingId;
					info.strName = listEnrollFieldName.get(i).strName;
					info.strDesc = listEnrollFieldName.get(i).strDesc;
					info.nFieldId = 0;
					info.eType = EnrollFieldInfo.EType.Text;
					info.strOptions = "";

					int nStatus = joinedJson.getInt(info.strName);
					if (nStatus == 0) {
						info.eStatus = EnrollFieldInfo.EStatus.Required;
					} else if (nStatus == 1) {
						info.eStatus = EnrollFieldInfo.EStatus.Optional;
					} else {
						info.eStatus = EnrollFieldInfo.EStatus.Hide;
					}

					DataCenter.getInstance().listEnrollField
							.addEnrollFieldInfo(info);
				}
			}
		} catch (JSONException ex) {
			// 无法进行报名
			fInfo.bIsJoined = true;
		}

		try {

			// 检查自定义字段
			JSONArray customFieldJson = dataJson.getJSONArray("customfields");
			if (customFieldJson != null) {
				for (int i = 0, n = customFieldJson.length(); i < n; i++) {
					JSONObject jData = customFieldJson.getJSONObject(i);

					EnrollFieldInfo info = new EnrollFieldInfo();
					info.nFieldId = jData.getInt("FieldId");
					info.strName = jData.getString("Title");
					info.strDesc = info.strName;
					info.strOptions = jData.getString("Options");
					info.eStatus = EnrollFieldInfo.EStatus.Required;

					int nType = jData.getInt("FieldType");
					if (nType == 0) {
						info.eType = EnrollFieldInfo.EType.Radio;
					} else if (nType == 1) {
						info.eType = EnrollFieldInfo.EType.CheckBox;
					} else {
						info.eType = EnrollFieldInfo.EType.Text;
					}

					DataCenter.getInstance().listEnrollField
							.addEnrollFieldInfo(info);
				}
			}

		} catch (JSONException ex) {
			fInfo.bIsJoined = true;
		}

		DataCenter.getInstance().listEnrollField.nMeetingId = nMeetingId;

		setErr(HttpUtil.ERROR_SUCCESS, mapData);
		return mapData;

	}
}
